package billingServer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.rmi.NoSuchObjectException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.security.MessageDigest;

import common.RegistryManagement;
import common.rmi.*;

public class BillingServer extends UnicastRemoteObject implements RemoteBillingServer {
	
	private static final long serialVersionUID = 1L;
	private BillingServerSecure billingServerSecure;

	protected BillingServer() throws RemoteException 
	{
		super(0);
		billingServerSecure = new BillingServerSecure();
	}


	public static void main(String[] args)
	{
		try {
			String _registaryName = args[0];
			BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
			Registry registry = RegistryManagement.createIfNotExists();
			if (registry != null)
			{
				BillingServer server= new BillingServer();
		    	registry.rebind(_registaryName, server);
		        System.out.println("BillingServer is bound");
				while(true){
					if(reader.readLine().trim().equals("!exit")) {
						System.out.println(exit(server));
						break;
					}
				}
			}
		} catch (Exception e) {
	    	System.err.println("BillingServer exception:" + e.getMessage());
	        e.printStackTrace();
		}
	}

	private static String exit(BillingServer server){
		try {
			UnicastRemoteObject.unexportObject(server.billingServerSecure,true);
		} catch (NoSuchObjectException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			UnicastRemoteObject.unexportObject(server, true);
		} catch (NoSuchObjectException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "Succesfully unexported the BillingServerSecure";
	}
	@Override
	public RemoteBillingServerSecure login(String userName, String password) throws RemoteException
	{
		BillingServerSecure res = null;
		
		java.io.InputStream is = ClassLoader.getSystemResourceAsStream("user.properties");
		if (is != null) 
		{
			java.util.Properties props = new java.util.Properties();
			try 
			{
				props.load(is);
				String savedPassword = props.getProperty(userName);
				if (savedPassword != null)
				{
					MessageDigest digest = MessageDigest.getInstance("MD5");

					//Update input string in message digest
			        digest.update(password.getBytes(), 0, password.length());
			        
			        //Converts message digest value in base 16 (hex) 
			        String hashedPassword = new BigInteger(1, digest.digest()).toString(16);
			        
					if (savedPassword.compareTo(hashedPassword) == 0)
					{
						res = billingServerSecure;	
					}
					else
						throw new RemoteException("Wrong password!");
				}
				else
					throw new RemoteException("User not found!");
				
				is.close();
			}
			catch (Exception e) {
				throw new RemoteException(e.getMessage());
			}
		}
		else 
		{
			throw new RemoteException("Properties file not found!");
		}

		return res;
	}
}
